package com.eportal.action.background;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.FeeAlipayAuth;
import com.eportal.domain.FeeMain;
import com.eportal.domain.FixFixer;
import com.eportal.domain.FuncMain;
import com.eportal.domain.Sysmenu;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TCommunity;
import com.eportal.domain.TOpenLog;
import com.eportal.exception.MdKnownRunTimeException;
import com.eportal.service.FeeAlipayAuthService;
import com.eportal.service.FixService;
import com.eportal.service.FuncAgentService;
import com.eportal.service.FuncApplyService;
import com.eportal.service.FuncMainService;
import com.eportal.service.TCommunityService;
import com.eportal.util.CityUtil;
import com.eportal.util.Config;
import com.eportal.util.HomePageConfig;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

/**
 * web功能应用管理
 * @author 
 *
 */
public class FuncMainAction extends BaseAction implements ModelDriven<FixFixer> {

	private static Logger log = Logger.getLogger(FuncMainAction.class.getName());
    
	private static final long serialVersionUID = 1L;

    private Map<String, Object> result = new HashMap<String, Object>();
    
    private static String FIXURL="/admin/fix_listView.action";
    @Autowired
    FuncMainService funcMainService;
    @Autowired
    TCommunityService communityService;
    @Autowired
    FeeAlipayAuthService feeAlipayAuthService;
    @Autowired
    FuncAgentService funcAgentService;
    @Autowired
    FuncApplyService funcApplyService;
    /**
     * 
     */
    public String listView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
    	String homePageConfig = HomePageConfig.FUNC_APPLY;
    	String[] str = homePageConfig.split(",");
		getRequest().setAttribute("mpid", Integer.parseInt(str[0]));
    	getSession().setAttribute("mpid", Integer.parseInt(str[0]));
        return SUCCESS;
    }
    
    /**
     * 申请开通列表
     */
	public void page() throws Exception {
		try {
			JqGridPager jqGridPager = new JqGridPager();
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			int userRole = (Integer) getSession().getAttribute("userRole");
			StringBuilder where = new StringBuilder(" ");
			jqGridPager.setWhere(where.toString());
			setPage(getRequest(), jqGridPager);
			List<Map<String, Object>> list = funcMainService.list4agent(jqGridPager, user.getAgtCode());
			jqGridQueryMap(Map.class, getRequest(), list, getResponse(), jqGridPager);
		} catch (Exception e) {
			log.error(this, e);
		}
	}
    
	
	public String applyView(){
		try{
			String sid = getRequest().getParameter("sid");
			FuncMain main = funcMainService.get(Long.valueOf(sid));
			getRequest().setAttribute("cname", main.getCname());
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			List<Map<String, Object>> list = communityService.listBySql("select * from t03_community where agtcode='"+user.getAgtCode()+"' and status='1' and sid not in (select communityid from func_detail where main_id='"+sid+"') and sid not in (select communityid from func_apply where (status='1' or status='2') and main_id='"+sid+"')");
			List<FeeAlipayAuth> aliList = feeAlipayAuthService.list(user.getAgtCode(),"2");
			getRequest().setAttribute("sid", sid);
			getRequest().setAttribute("aliList", aliList);
			getRequest().setAttribute("datalist", list);
		}catch(Exception e){
			log.error(this, e);
		}
		return SUCCESS;
	}
    
	
	public String listView4audit(){
		try{
			int userRole =  (Integer) getSession().getAttribute("userRole");
	    	getRequest().setAttribute("userRole", userRole);
	    	String homePageConfig = HomePageConfig.FUNC_AUTH;
	    	String[] str = homePageConfig.split(",");
			getRequest().setAttribute("mpid", Integer.parseInt(str[0]));
	    	getSession().setAttribute("mpid", Integer.parseInt(str[0]));
		}catch(Exception e){
			log.error(this, e);
		}
		return SUCCESS;
	}
	
	public void page4audit(){
		try{
			JqGridPager jqGridPager = new JqGridPager();
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			int userRole = (Integer) getSession().getAttribute("userRole");
			String agtCode = getRequest().getParameter("agtCode");
			String funcMainId = getRequest().getParameter("funcMainId");
			StringBuilder where = new StringBuilder(" ");
			jqGridPager.setWhere(where.toString());
			setPage(getRequest(), jqGridPager);
			List<Map<String, Object>> list = funcAgentService.list(jqGridPager,agtCode,funcMainId);
			jqGridQueryMap(Map.class, getRequest(), list, getResponse(), jqGridPager);
		}catch(Exception e){
			log.error(this, e);
		}
		return ;
	}
	
	
	public void apply(){
		Map map = new HashMap();
		try{
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			String sid = getRequest().getParameter("sid");
			String communityids = getRequest().getParameter("communityids");
			String alipayIds = getRequest().getParameter("alipayIds");
			String[] communityArr = communityids.split(",");
			String[] alipayArr = alipayIds.split(",");
			funcMainService.apply(communityArr,alipayArr,sid,user.getAgtCode(),user.getSid());
			map.put("code", "1");
			map.put("msg", "操作成功");
		}catch(MdKnownRunTimeException e){
			map.put("code", "-999");
			map.put("msg", e.getMessage());
			log.error(this, e);
		}catch(Exception e){
			map.put("code", "-9999");
			map.put("msg", "服务器繁忙，请稍后再试");
			log.error(this, e);
		}
		responseBody(map);
		return ;
	}
    
	
	public void audit(){
		Map map = new HashMap();
		try{
			String result = getRequest().getParameter("result");
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			String applyids = getRequest().getParameter("applyids");
			String comment = getRequest().getParameter("comment");
			String[]  applyArr = applyids.split(",");
			if("yes".equals(result)){
				funcApplyService.auditYes(applyArr,user.getSid());	
			}else if("no".equals(result)){
				funcApplyService.auditNo(applyArr,user.getSid(),comment);	
			}
			
			
			map.put("code", "1");
			map.put("msg", "操作成功");
		}catch(MdKnownRunTimeException e){
			map.put("code", "-999");
			map.put("msg", e.getMessage());
			log.error(this, e);
		}catch(Exception e){
			map.put("code", "-9999");
			map.put("msg", "服务器繁忙，请稍后再试");
			log.error(this, e);
		}
		responseBody(map);
		return ;
	}
	
	public String viewDetailByAdmin(){
		try{
			String sid = getRequest().getParameter("sid");
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			List<Map<String, Object>> list = funcMainService.listDetailByAdmin(sid);
			getRequest().setAttribute("datalist", list);
		}catch(Exception e){
			log.error(this, e);
		}
		return SUCCESS;
	}
	
	public String viewDetail(){
		try{
			String sid = getRequest().getParameter("sid");
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			List<Map<String, Object>> list = funcMainService.listDetail(sid,user.getAgtCode());
			getRequest().setAttribute("datalist", list);
		}catch(Exception e){
			log.error(this, e);
		}
		return SUCCESS;
	}
    
	
	public String auditView(){
		try{
			String sid = getRequest().getParameter("sid");
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			List<Map<String, Object>> list = funcApplyService.list4audit(sid);
			getRequest().setAttribute("datalist", list);
		}catch(Exception e){
			log.error(this, e);
		}
		return SUCCESS;
	}

	@Override
	public FixFixer getModel() {
		// TODO Auto-generated method stub
		return null;
	}
	
 

 
}
